/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef TRAINER_H_
#define TRAINER_H_

#include <fann.h>

#include <vector>
using namespace std;

#include <QList>
#include <QThread>
#include <QTimer>

#include "SQLiteDB.h"

#define INFINITE 0xFFFFFFFF
#define INPUT_SIZE 4

class TrainerThread : public QThread
{
	Q_OBJECT

public:
	TrainerThread(QObject * parent = 0);
	virtual ~TrainerThread();

	void setTrainData();
	float testANN(QString annFile = QString());
	void exit(int returnCode = 0);

	void simAvg();

public slots:
	void trainNextEpoch();
	void testWord(int id, int size);

signals:
	void updateMSE(int, float);
	void statusChanged(const QString&);
	void updateWordSim(QString, vector<int>, vector<float>); //updateWordSim(word, intv, predict)

protected:
	virtual void run();

private:
	void startTraining();
	void stopTraining();
	bool checkTrainingFinished();
	void clearTestDataVect();

	void simSingleWord(
			QString word,
			QString test_records,
			int googleSearchCount,
			int size,
			fann *ann,
			vector<int>& intvVect,
			vector<float>& simValVect);

	int calculateScore(const QStringList& recordList);

	float simInput(
			fann *ann,
			int testCount,
			int testIntv,
			int testScore,
			int googleSearchCount);

private:
	int mEpochMS;
	int mCurEpoch;
	bool mIsTraining;
	QTimer mTrainTimer;
	float mInitMSE;
	float mFinalMSE;
	vector<float> mMSEVect;

	QString mTrainDataFile;
	QString mAnnFile;
	struct fann_train_data* mTrainData;
	struct fann* mAnn;
	QStringList mInputNameList;
	QString mInputNameTestCount;
	QString mInputNameIntv;
	QString mInputNameScore;
	QString mInputNameGoogleCount;
	vector<fann_type*> mTestInputVect;
	vector<int> mTestOutVect;
};

#endif /* TRAINER_H_ */
